﻿using System;
using System.IO;
using System.Text;

namespace MediaAssistant.DAL.Helper
{
    public class Logger
    {
        private readonly static object LockObject=new object();
        private static volatile Logger _instance ;
        public static Logger Instance
        {
            get
            {
                if(_instance==null)
                {
                    lock (LockObject)
                    {
                        if(_instance==null)
                        {
                            _instance=new Logger();
                        }
                    }
                }
                return _instance;
            }
        }
        public void Write(string message)
        {
            lock (LockObject)
            {
                try
                {
                    var logDirectory = GetLogDirectory();
                    var logWriter = File.AppendText(GetLogFileName(logDirectory));
                    logWriter.WriteLine(String.Format("{0}: {1}", DateTime.Now, message));
                    logWriter.Close();    
                }
                catch (Exception)
                {
                    
                }
                
            }
        }

        private static string GetLogFileName(string logDirectory)
        {
            var index = 1;
            while(true)
            {
                var logFile = Path.Combine(logDirectory, String.Format("MediaAssistant{0:000}.log", index++));
                var fileInfo = new FileInfo(logFile);
                if(fileInfo.Exists && fileInfo.Length>=1024*1024)
                {
                    continue;
                }
                return logFile;
            }
        }

        private static string GetLogDirectory()
        {
            var appDataLocation = SystemHelper.GetAppDataLocation();
            var logDirectory = Path.Combine(appDataLocation, "Log");
            if (Directory.Exists(logDirectory) == false)
            {
                Directory.CreateDirectory(logDirectory);
            }
            return logDirectory;
        }

        public static string GetExceptionMessage(Exception e)
        {
            var newLine = Environment.NewLine;
            // Preparing the string 
            var sb = new StringBuilder();
            sb.Append("Exception type: " + e.GetType().AssemblyQualifiedName + newLine);
            sb.Append("Exception message: " + e.Message);
            var innerException = e.InnerException;
            var counter = 1;
            while (null != innerException)
            {
                sb.AppendFormat("{0}Inner exception {1} type: {2}", newLine, counter, innerException.GetType().AssemblyQualifiedName);
                sb.AppendFormat("{0}Inner exception {1} message: {2}", newLine, counter, innerException.Message);
                counter++;
                innerException = innerException.InnerException;
            }
            return sb.ToString();
        }

        public void Write(Exception exception)
        {
            Write(GetExceptionMessage(exception));
        }
    }
}

